var Data = function(){
	var _this = this;
	this.FIELDS_NUM;
	this.columns = null;
	this.minY, this.maxY;
	this.korgMax = 20000;
	this.korgMin=-20000;
	this.stopCam = 0;
	this.startCam = 0;
	this.xtrms = [];
	this.sagedus = 10;
	this.isNew = false;
	this.korgVahe=null;
	this.parsed = null;
	this.sensorsData = [];
	this.resOut = function(arr){
		this.sensorsData = arr;
		if (this.sensorsData.length) console.log("READY");
	    this.isNew = true;
		this.fileToData();
	};
	
	this.fileToData = function(){
		if (this.sensorsData.length==0) {
			console.log("NOT READY YET");
			return;
		}
		this.columns = this.sensorsData[0].split(","); // massiv polej CSV-faila
		this.FIELDS_NUM = this.getFieldsNum(this.columns);
		var colCount=this.columns.length;
		var objNum = Math.floor((this.columns.length-1)/this.FIELDS_NUM);
		var obData=[], maxMin = []; this.xtrms=[]        // massiv objektov
		var ir = 0;
		for (var i=0; i < objNum; i++){ // prokrutim po chislu uzlov
			//gControls.boxes(i, objNum);
			var Node = {}; // objekt Node
			var NodeX = {}; // objekt dlja xtrmumov
			var maxMins = {}; // objekt dlja max i min
	        obData.push(Node);
	        this.xtrms.push(NodeX);
	        maxMin.push(maxMins);
	        for (var j=0; j<(this.FIELDS_NUM+1); j++){
	        	Node[this.columns[j]]=[]; // kazhdoe pole objekta -- massiv
				NodeX[this.columns[j]]=[];
				maxMins[this.columns[j]] = []; 
	        }
		}
		    // PROKRUCHIVAEM FAIL sensorsData PO CHISLU STROK
		for (i=1; i < this.sensorsData.length; i++){
				// SNACHALA UBEREM VIDEO SYNC
			if (this.sensorsData[i].substring(1,2) == '@'){ //Kontrollime, kas rida pole video sync data
				this.setSync(this.sensorsData[i].split("@"));
				continue;
			}
			datArr=this.sensorsData[i].split(",");
			for(j=1; j<this.columns.length; j++){ // Prokrutim po chislu polej CSV, krome abs_time
				var sensIndex = Math.floor((j-1)/this.FIELDS_NUM);
				var fysIndex = this.columns[j%this.FIELDS_NUM];
				if(j%this.FIELDS_NUM==0) { // ESLI U NAS CHISLO IZ POSLEDNEJ KOLONKi
					fysIndex= this.columns[this.FIELDS_NUM];
					obData[sensIndex][fysIndex].push(datArr[j]); //ZAPISHEM DANNYE
					obData[sensIndex][this.columns[0]].push(datArr[0]); // I ABS TIMESTAMP
				} else obData[sensIndex][fysIndex].push(datArr[j]); // ESLI NET -- ZAPISHEM CHISLO
				last=obData[sensIndex][fysIndex].length-1;
					
					// ISHEM MAX I MIN
				if (last==0){
					maxMin[sensIndex][fysIndex][0]=obData[sensIndex][fysIndex][last];
					maxMin[sensIndex][fysIndex][1]=obData[sensIndex][fysIndex][last];
					maxMin[sensIndex][fysIndex][2]=obData[sensIndex][fysIndex][last]-0;
				} else {
					if (obData[sensIndex][fysIndex][last]-0>maxMin[sensIndex][fysIndex][0]-0) // ishem maximum
						maxMin[sensIndex][fysIndex][0]=obData[sensIndex][fysIndex][last];
					if (obData[sensIndex][fysIndex][last]-0<maxMin[sensIndex][fysIndex][1]-0) // ishem minimum
						maxMin[sensIndex][fysIndex][1]=obData[sensIndex][fysIndex][last];
						maxMin[sensIndex][fysIndex][2]+=obData[sensIndex][fysIndex][last]-0;
					}
					if (i==this.sensorsData.length-1) {maxMin[sensIndex][fysIndex][3]=maxMin[sensIndex][fysIndex][2]/(i-2);} // i-2 -- t.k. 1 strochka zagolovkov
					
					// SOHRANIM VSE EXTREMUMY V MASSIV (xtrms[sensor][fys][i])
					if (obData[sensIndex][fysIndex].length >3) { 
						if (obData[sensIndex][fysIndex][last-1]-0>obData[sensIndex][fysIndex][last-2]-0 &&
						    obData[sensIndex][fysIndex][last-1]-0>obData[sensIndex][fysIndex][last]-0) {
							this.xtrms[sensIndex][fysIndex][last-1] = obData[sensIndex][fysIndex][last-1];
						}
						if (obData[sensIndex][fysIndex][last-1]-0<obData[sensIndex][fysIndex][last-2]-0 &&
						    obData[sensIndex][fysIndex][last-1]-0<obData[sensIndex][fysIndex][last]-0) {
							this.xtrms[sensIndex][fysIndex][last-1] = obData[sensIndex][fysIndex][last-1];
						}
					}
	               }
	            }
		     // TEPER UBEREM EXTREMUMY, KOTORYE KOLEBLJUTSJA OKOLO SREDNEGO
		    for (i = 0; i < this.xtrms.length; i++){ // prokrutim massiv extremumov: a) po sensoram
				for (j=1;j<this.FIELDS_NUM+1;j++){ // b) po fys. parametram
					var fysIx = this.columns[j]; // fys. parametr
					var gate = (maxMin[i][fysIx][0]-maxMin[i][fysIx][1])/100; // 1% ot diapazona krivoj
					var gateMax = maxMin[i][fysIx][3]+ gate; // srednee + 1%
					var gateMin = maxMin[i][fysIx][3]- gate; // srednee - 1%
					for (var y=0; y<this.xtrms[i][fysIx].length; y++){
						if (!this.xtrms[i][fysIx][y]) continue;
						if (obData[i][fysIx][y]-0 <gateMax && obData[i][fysIx][y]-0>gateMin) {
							this.xtrms[i][fysIx][y]=null;
						}
					}
				}
		    }
	          this.parsed = obData;
	          if (this.parsed) { // leiame
	               var array = this.parsed[0][this.columns[4]];
	               this.minY = array[0], this.maxY=0;
	               for (var i=0; i<array.length; i++){
	                    if (typeof array[i] === 'number' && array[i]<this.minY) this.minY =array[i];
	                    
	                    if (array[i]>this.maxY) this.maxY=array[i];
	               }
	          }
		  this.sagedus=this.parsed[0]['absolute timestamp'][1]- this.parsed[0]['absolute timestamp'][0];

	};
	this.setMinMax = function(){
		var curvBxs = $("#curveControls :checkbox[name=curve]:checked");
        var absMax, absMin;
        for (var i=0;i<curvBxs.length; i++){
             var snsNum = curvBxs[i].id.split("_")[1];
             var fys = curvBxs[i].id.split("_")[0];
             if (i==0) absMax = absMin = this.parsed[snsNum][fys][0];
             var curvMax = this.parsed[snsNum][fys][0];
             var curvMin = curvMax;
             for (var j = 1; j<this.parsed[snsNum][fys].length; j++){
                  if  (this.parsed[snsNum][fys][j]-0>curvMax) curvMax=this.parsed[snsNum][fys][j]-0;
                  if  (this.parsed[snsNum][fys][j]-0<curvMin) curvMin=this.parsed[snsNum][fys][j]-0;
             }
             //console.log ("fys: "+fys+"; curvMax: "+curvMax + "; curvMin: "+curvMin);
             if (curvMax > absMax) absMax=curvMax;
             if (curvMin < absMin) absMin=curvMin;
        }
        //console.log("absMax: "+absMax+"; absMin: "+absMin);
        this.korgMax=absMax;
        this.korgMin = absMin;
        this.korgVahe = this.korgMax-this.korgMin;
     //   this.graph.setVer();
	};
	this.tickY = function(){
		return (this.korgMax-this.korgMin)/8;
	};
	this.getFieldsNum = function(arr){ // TAGASTAB V"ALJADE ARVU, KUI LEIAB VA"LJADE KORDUMIST
		for (var i = 2; i < arr.length; i++){
	    	 if (arr[i] == arr[1]) return i-1;
	    }
		return i-1;
	};
	this.setSync = function(syncArr){
	     if (syncArr[1]=="s") this.startCam=syncArr[2]-0;
	     else if (syncArr[1]=="e") this.stopCam=syncArr[2]-0;
	};
	this.getTime = function(){
        if (isNaN(video.duration)) return 0;
        return Math.round(this.startCam-0+video.currentTime*1000);
   };

};